home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Audio dcmds / Audio CD dcmds.sit / Audio CD dcmds.π / play dcmd ƒ / dcmd.c < prev    next >
Text File  |  1995-07-24  |  7KB  |  342 lines

  1. /*****************************************************
  2.  
  3.     bin.c                    P. Stadelmann, June 1994
  4.     
  5.     Displays a long word in binary.
  6.  
  7.  *****************************************************/
  8.  
  9.  
  10. #include <SetUpA4.h>    //dcmd.h
  11. #include <Think_dcmd.h>
  12. #include <Think_put.h>
  13. #include "cd.h"
  14. #include "toolutils.h"
  15. #define min(x,y) (x < y ? x : y )
  16. #define max(x,y) (x > y ? x : y )
  17.  
  18. void    PlayTrack(Byte startTrack);
  19. pascal Boolean    BitTest(const void *    data, long bitNum);
  20. void     DoAudioCommand(short command);
  21.  
  22. void DoHelp( void )
  23. {
  24.     PutPStr( "\pplay [Track # | P | E | S | ? | + | - | v vol]");
  25.     PutLine();
  26.     PutPStr( "\p   Starts playing an audio CD. ");
  27.     PutLine();
  28.     PutPStr( "\p   Track # - Start playing from the provided track number");
  29.     PutLine();
  30.     PutPStr( "\p   P - Pause or resume play");
  31.     PutLine();
  32.     PutPStr( "\p   E - Eject the disk");
  33.     PutLine();
  34.     PutPStr( "\p   S - Stop playing");
  35.     PutLine();
  36.     PutPStr( "\p   ? - Show current track and time info");
  37.     PutLine();
  38.     PutPStr( "\p   + - Skip to the next track");
  39.     PutLine();
  40.     PutPStr( "\p   - - Skip to the previous track");
  41.     PutLine();
  42.     PutPStr( "\p   v volume - Change volume to the provided level");
  43.     PutLine();
  44. }
  45.  
  46. void DoJob()
  47. {
  48.         long        value;
  49.         Boolean        ok;
  50.         Str255        myStr;
  51.         short        pos;
  52.     
  53.     switch(dcmdPeekAtNextChar()) {
  54.         case '\r':
  55.             PlayTrack(1);
  56.             break;
  57.         
  58.         case 'P':
  59.         case 'E':
  60.         case 'S':        
  61.         case 'p':
  62.         case 'e':
  63.         case 's':        
  64.         case '?':
  65.         case '+':
  66.         case '-':
  67.             dcmdGetNextParameter(myStr);
  68.             //pos = dcmdGetPosition();
  69.             //dcmdGetNextExpression( &value, &ok );
  70.             DoAudioCommand(myStr[1]);
  71.             break;
  72.             
  73.         case 'V':
  74.         case 'v':
  75.             dcmdGetNextParameter(myStr);
  76.             pos = dcmdGetPosition();
  77.             dcmdSetPosition(pos);
  78.             dcmdGetNextExpression( &value, &ok );
  79.             if(ok && value >= 0 && value <= 255) {
  80.                 OSErr    osErr;
  81.                 short ioRefNum;
  82.                 osErr = OpenCD(1, &ioRefNum);
  83.                 
  84.                 if(osErr != noErr) {
  85.                     PutLine();
  86.                     PutPStr( "\pCouldn't open CD Drive!");
  87.                     return;
  88.                 }
  89.                 if(!DiscInDrive(ioRefNum)) {
  90.                     PutLine();
  91.                     PutPStr( "\pNo disk in CD Drive!");
  92.                     return;
  93.                 }
  94.  
  95.                 PutPStr( "\pSetting volume to ");
  96.                 PutSDec(value);
  97.                 ASetVolume(ioRefNum, value);
  98.             }
  99.             else {
  100.                 PutPStr( "\pVolume not found or out of range");
  101.             }
  102.             break;
  103.             
  104.         default:
  105.             {
  106.                 pos = dcmdGetPosition();
  107.                 dcmdGetNextExpression( &value, &ok );
  108.         
  109.                 if (ok)
  110.                 {
  111.                     PutPStr( "\pstarting at track ");
  112.                     PutSDec(value);
  113.                     PutPStr("\p…");
  114.                     PlayTrack(value);        
  115.                 }
  116.                 else
  117.                     PutPStr( "\pSyntax error");
  118.             }
  119.             break;
  120.     }
  121.  
  122.     PutLine();
  123.  
  124. }
  125.  
  126. void SetVolume(short value)
  127. {
  128.  
  129. }
  130.  
  131. void     DoAudioCommand(short command)
  132. {
  133.     OSErr    osErr;
  134.     short    drive,
  135.             ioRefNum,
  136.             count,
  137.             track,
  138.             state;
  139.     Byte    userCDNumber,
  140.             userStart, 
  141.             userStop,
  142.             discStart,
  143.             discStop,
  144.             audioStatus,
  145.             playMode,
  146.             cntlField,
  147.             minutes,
  148.             seconds,
  149.             frames;
  150.     Byte        trackMin, trackSec, trackFrame;
  151.     Byte    currentTrack, discMin, discSec, discFrame;
  152.  
  153.  
  154.     userCDNumber    = 1;
  155.     userStart        = 0;
  156.     userStop        = 100;
  157.     playMode        = STEREO;
  158.  
  159.     osErr = OpenCD(userCDNumber, &ioRefNum);
  160.     if(osErr != noErr) {
  161.         PutLine();
  162.         PutPStr( "\pCouldn't open CD Drive!");
  163.         return;
  164.     }
  165.     if(!DiscInDrive(ioRefNum)) {
  166.         PutLine();
  167.         PutPStr( "\pNo disk in CD Drive!");
  168.         return;
  169.     }
  170.      
  171.     osErr = AStatus(ioRefNum, &audioStatus, &playMode, &cntlField, &minutes, &seconds, &frames);
  172.     
  173.     discStart = 0; 
  174.     do 
  175.         osErr = TrackInfo(ioRefNum, ++discStart, &cntlField, &minutes, &seconds, &frames);
  176.     while (BitTst(&cntlField, 5) && noErr == osErr); 
  177.     osErr = TrackCount(ioRefNum, &discStop);
  178.     osErr = ACurrTrackInfo(ioRefNum, ¤tTrack, &trackMin, &trackSec, &trackFrame);
  179.  
  180.  
  181.     switch (command) {
  182.         case 'p':
  183.         case 'P':
  184.             if(audioStatus == PAUSED) {
  185.                 PutPStr( "\pResuming");
  186.                 osErr = APause(ioRefNum, CONTINUE);
  187.             } else {
  188.                 if(audioStatus == PLAYING) {
  189.                     PutPStr( "\pPausing");
  190.                     osErr = APause(ioRefNum, PAUSE);
  191.                 }
  192.             }
  193.             break;
  194.             
  195.         case 'S':
  196.         case 's':
  197.             PutPStr( "\pStopping");
  198.             osErr = AStop(ioRefNum, 0);
  199.             break;
  200.     
  201.         case '+':
  202.             osErr = ATrkSearch( ioRefNum, ++currentTrack, audioStatus, playMode);
  203.             PutPStr( "\pswitching to track ");
  204.             PutSDec(currentTrack);
  205.             osErr = APause(ioRefNum, audioStatus);
  206.             break;
  207.         
  208.         case '-':
  209.             if((trackMin == 0) && (trackSec <=10)) {
  210.                 osErr = ATrkSearch( ioRefNum, --currentTrack, audioStatus, playMode);
  211.             }
  212.             else {
  213.                 osErr = ATrkSearch( ioRefNum, currentTrack, audioStatus, playMode);
  214.             }
  215.             PutPStr( "\pswitching to track ");
  216.             PutSDec(currentTrack);
  217.             osErr = APause(ioRefNum, audioStatus);
  218.             break;
  219.             
  220.         case '?':
  221.             PutPStr( "\pTrack ");
  222.             PutSDec(currentTrack);
  223.             PutPStr( "\p  ");
  224.             PutSDec(trackMin);
  225.             PutPStr( "\p:");
  226.             PutSDec(trackSec);
  227.             break;
  228.             
  229.         case 'E':
  230.         case 'e':
  231.             PutPStr( "\pEjecting disk");
  232.             osErr = EjectCD(ioRefNum);
  233.             break;    
  234.     }
  235.     
  236. }
  237.  
  238.  
  239. void    PlayTrack(Byte startTrack) 
  240. {
  241.     OSErr    osErr;
  242.     short    drive,
  243.             ioRefNum,
  244.             count,
  245.             track,
  246.             state;
  247.     Byte    userCDNumber,
  248.             userStart, 
  249.             userStop,
  250.             discStart,
  251.             discStop,
  252.             audioStatus,
  253.             playMode,
  254.             cntlField,
  255.             minutes,
  256.             seconds,
  257.             frames;
  258.     userCDNumber    = 1;
  259.     userStart        = 0;
  260.     userStop        = 100;
  261.     playMode        = STEREO;
  262.  
  263.     osErr = OpenCD(userCDNumber, &ioRefNum);
  264.     if(osErr != noErr) {
  265.         PutLine();
  266.         PutPStr( "\pCouldn't open CD Drive!");
  267.         return;
  268.     }
  269.     if(!DiscInDrive(ioRefNum)) {
  270.         PutLine();
  271.         PutPStr( "\pNo disk in CD Drive!");
  272.         return;
  273.     }
  274.  
  275.     osErr = AStatus(ioRefNum, &audioStatus, &playMode, &cntlField, &minutes, &seconds, &frames);
  276.     if(audioStatus == PAUSED) {
  277.         osErr = APause(ioRefNum, CONTINUE);
  278.     } else {
  279.     
  280.         discStart = 0; 
  281.         do 
  282.             osErr = TrackInfo(ioRefNum, ++discStart, &cntlField, &minutes, &seconds, &frames);
  283.         while (BitTst(&cntlField, 5) && noErr == osErr); 
  284.         osErr = TrackCount(ioRefNum, &discStop);
  285.  
  286.         if (noErr == osErr) {
  287.             if (startTrack == 0)  {
  288.                 osErr = AStop(ioRefNum, 0);
  289.             }
  290.             else {            
  291.                 osErr = AStop(ioRefNum, max(min(userStop, discStop), discStart));
  292.                 if(startTrack > 0) {
  293.                     userStart = startTrack;
  294.                     osErr = APlay(ioRefNum, startTrack, playMode);
  295.                 }
  296.                 else{
  297.                     osErr = APlay(ioRefNum, min(max(userStart, discStart), discStop), playMode);
  298.                 }
  299.             }
  300.         }
  301.     }
  302.     if(osErr) {
  303.         PutPStr( "\pGot error trying to play ");
  304.         PutSDec(osErr);
  305.     }
  306. }
  307.  
  308. //
  309. /*
  310. pascal Boolean    BitTest(const void *    data, long bitNum)
  311. {
  312.     unsigned long mask;
  313.     unsigned long    value;
  314.     
  315.     if(bitNum > 31) return false;
  316.     
  317.     bitNum = 31 - bitNum;
  318.     
  319.     value = *((unsigned long *)data);
  320.     mask = 1 << bitNum;
  321.     return ((value & mask) != 0);
  322. }
  323. */
  324.  
  325. pascal void CommandEntry( dcmdBlock* paramPtr )
  326. {
  327.     RememberA0();
  328.     SetUpA4();    
  329.     
  330.     switch ( paramPtr->request )
  331.     {
  332.         case dcmdInit : break;
  333.         
  334.         case dcmdHelp : DoHelp();
  335.                         break;
  336.                         
  337.         case dcmdDoIt : DoJob();
  338.                         break;
  339.     }
  340.  
  341.     RestoreA4();
  342. }